package com.zp.base.impl.examMaintenance;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.api.bo.ClinicItemDictBo;
import com.zp.base.api.vo.ClinicItemDictVo;
import com.zp.base.api.vo.ExamVsClinicVo;
import com.zp.base.entity.examMaintenance.ExamVsClinic;
import com.zp.base.entity.examMaintenance.bo.ExamVsClinicBo;
import com.zp.base.mapper.examMaintenance.ExamVsClinicMapper;
import com.zp.base.service.clinicItemDict.IClinicItemDictService;
import com.zp.base.service.examMaintenance.IExamVsClinicService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 检查项目与诊疗项目对照Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-05-27
 */
@RequiredArgsConstructor
@Service
public class ExamVsClinicServiceImpl implements IExamVsClinicService {

    private final ExamVsClinicMapper baseMapper;

    private final IClinicItemDictService clinicItemDictService;

    /**
     * 查询检查项目与诊疗项目对照
     */
    @Override
    public ExamVsClinicVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询检查项目与诊疗项目对照列表(分页)
     */
    @Override
    public TableDataInfo<ExamVsClinicVo> queryPageList(ExamVsClinicBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<ExamVsClinic> lqw = buildQueryWrapper(bo);
//        Page<ExamVsClinicVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<ExamVsClinicVo> result = baseMapper.selectVoPage(pageQuery.build(), bo);
        result.getRecords().forEach(res -> {
            res.setExamPositionArray(Arrays.asList(res.getExamPosition().split(",")));
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询检查项目与诊疗项目对照列表(不分页)
     */
    @Override
    public List<ExamVsClinicVo> queryList(ExamVsClinicBo bo) {
        LambdaQueryWrapper<ExamVsClinic> lqw = buildQueryWrapper(bo);
        lqw.eq(ExamVsClinic::getDelFlag, "0");
        List<ExamVsClinicVo> examVsClinicVos = baseMapper.selectVoList(lqw);
        ClinicItemDictBo clinicItemDictBo = new ClinicItemDictBo();
        clinicItemDictBo.setOrgId(bo.getOrgId());
        // 查询诊疗项目总价
        List<ClinicItemDictVo> clinicItemDictVos = clinicItemDictService.queryList(clinicItemDictBo);
        examVsClinicVos.forEach(examVsClinicVo -> {
            examVsClinicVo.setExamPositionArray(Arrays.asList(examVsClinicVo.getExamPosition().split(",")));
            // 根据检查项目的项目id 获取 诊疗项目总价集合
            List<ClinicItemDictVo> collect = clinicItemDictVos.stream().filter(x -> StringUtils.equals(x.getClinicItemId(),
                    examVsClinicVo.getClinicId())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                examVsClinicVo.setTotalPrice(ObjectUtil.isNotNull(collect.get(0).getSubTotal()) ? collect.get(0).getSubTotal() : 0.00);
                examVsClinicVo.setItemName(collect.get(0).getItemName());
                examVsClinicVo.setItemCode(collect.get(0).getItemCode());
                examVsClinicVo.setInputCode(collect.get(0).getInputCode());
            }
        });
        return examVsClinicVos.stream().filter(x -> StringUtils.isNotBlank(x.getItemName())).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<ExamVsClinic> buildQueryWrapper(ExamVsClinicBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamVsClinic> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getExamId()), ExamVsClinic::getExamId, bo.getExamId());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicId()), ExamVsClinic::getClinicId, bo.getClinicId());
        lqw.eq(StringUtils.isNotBlank(bo.getPerformedBy()), ExamVsClinic::getPerformedBy, bo.getPerformedBy());
        lqw.eq(StringUtils.isNotBlank(bo.getExamPosition()), ExamVsClinic::getExamPosition, bo.getExamPosition());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), ExamVsClinic::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), ExamVsClinic::getOrgId, bo.getOrgId());
        lqw.eq(bo.getSort() != null, ExamVsClinic::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), ExamVsClinic::getRemarks, bo.getRemarks());
        lqw.orderByAsc(ExamVsClinic::getSort);
        return lqw;
    }

    /**
     * 新增检查项目与诊疗项目对照
     */
    @Override
    public Boolean insertByBo(ExamVsClinicBo bo) {
        ExamVsClinic add = BeanUtil.toBean(bo, ExamVsClinic.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setVsId(add.getVsId());
        }
        return flag;
    }

    /**
     * 修改检查项目与诊疗项目对照
     */
    @Override
    public Boolean updateByBo(ExamVsClinicBo bo) {
        ExamVsClinic update = BeanUtil.toBean(bo, ExamVsClinic.class);
        update.setExamPosition(bo.getExamPositionArray().stream()
                .collect(Collectors.joining(",")));
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExamVsClinic entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检查项目与诊疗项目对照
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
